package org.linlinjava.litemall.db.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 时间工具类
 * @author: Destin
 * @date: 2021-09-27
 */
public class DateUtil {

    private static final String DATE_FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当天零点 时间
     * @return 当天零点 时间
     */
    public static  String currentZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIME);
        return sdf.format(zero);
    }

    /**
     * 获取当天24点 时间
     * @return 当天24点 时间
     */
    public static  String currentOverDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date zero = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIME);
        return sdf.format(zero);
    }

    public static  String currentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIME);
        return sdf.format(new Date());
    }

    public static  String yesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 00);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIME);
        return sdf.format(calendar.getTime());
    }

    public static  String tomorrow() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TIME);
        return sdf.format(calendar.getTime());
    }

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(DATE_FORMAT_TIME);
        return dtf2.format(dateTime);
    }

    public static void main(String[] args) {
        System.out.println(tomorrow());
    }

    public static boolean isBetween(LocalDateTime startTime, LocalDateTime endTime) {
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return false;
        }
        LocalDateTime nowTime= LocalDateTime.now();
        return  (nowTime.isBefore(endTime) && nowTime.isAfter(startTime));
    }
}
